package views.cad;

import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionListener;
import java.awt.event.FocusListener;
import java.awt.event.MouseListener;
import java.sql.Date;
import java.text.ParseException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;
import javax.swing.text.MaskFormatter;

import models.cad.Atividade;
import models.cad.Usuario;
import supers.GUIInternalFrame;
import supers.GUICadInterface;
import utils.GlobalDeclarations;
import views.SisDskGUI;

import javax.swing.JRadioButton;

@SuppressWarnings("serial")
public class CadAtvGUI extends GUIInternalFrame implements GUICadInterface {
	public JTextField tfNome;
	public JFormattedTextField tfCod;
	public JButton btNovo;
	public JButton btExcluir;
	public JButton btListar;
	public JTextField tfCH;
	public JRadioButton rbHoras;
	public JRadioButton rbSessoes;
	
	public CadAtvGUI() {
		setTitle("Cadastro de Atividades");
		setSize(new Dimension(335, 190));
		
		tfNome = new JTextField();
		tfNome.setBounds(71, 56, 226, 20);
		getContentPane().add(tfNome);
		tfNome.setColumns(10);
		try {
			tfCod = new JFormattedTextField(new MaskFormatter("###"));
			tfCod.setColumns(3);
			tfCod.setBounds(71, 25, 30, 20);
			getContentPane().add(tfCod);
			
			JLabel lblNewLabel = new JLabel("Cod.:");
			lblNewLabel.setLabelFor(tfCod);
			lblNewLabel.setBounds(27, 28, 34, 14);
			getContentPane().add(lblNewLabel);
			
			JLabel lblNewLabel_1 = new JLabel("Nome:");
			lblNewLabel_1.setLabelFor(tfNome);
			lblNewLabel_1.setBounds(27, 59, 46, 14);
			getContentPane().add(lblNewLabel_1);
			
			JPanel panel_1 = new JPanel();
			panel_1.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
			panel_1.setBounds(27, 118, 270, 26);
			getContentPane().add(panel_1);
			panel_1.setLayout(null);
			
			btNovo = new JButton("Novo");
			ImageIcon imageIcon = new ImageIcon(SisDskGUI.class.getResource(GlobalDeclarations.ImgBtNovo));
			Image img = imageIcon.getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH);
			btNovo.setIcon(new ImageIcon(img));
			btNovo.setBounds(1, 1, 80, 23);
			panel_1.add(btNovo);
			
			btExcluir = new JButton("Excluir");
			btExcluir.setEnabled(false);
			ImageIcon imageIcon2 = new ImageIcon(SisDskGUI.class.getResource(GlobalDeclarations.ImgBtExcluir));
			Image img2 = imageIcon2.getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH);
			btExcluir.setIcon(new ImageIcon(img2));
			btExcluir.setBounds(91, 1, 85, 23);
			panel_1.add(btExcluir);
			
			btListar = new JButton("Listar");
			ImageIcon imageIcon3 = new ImageIcon(SisDskGUI.class.getResource(GlobalDeclarations.ImgBtListar));
			Image img3 = imageIcon3.getImage().getScaledInstance(16, 16, Image.SCALE_SMOOTH);
			btListar.setIcon(new ImageIcon(img3));
			btListar.setBounds(186, 1, 80, 23);
			panel_1.add(btListar);
			
			JLabel lblCj = new JLabel("Carga:");
			lblCj.setBounds(27, 90, 34, 14);
			getContentPane().add(lblCj);
			
			tfCH = new JTextField();
			tfCH.setColumns(2);
			tfCH.setBounds(71, 87, 22, 20);
			getContentPane().add(tfCH);
			
			rbHoras = new JRadioButton("Horas");
			rbHoras.setSelected(true);
			rbHoras.setBounds(118, 86, 58, 23);
			getContentPane().add(rbHoras);
			
			rbSessoes = new JRadioButton("Sess\u00F5es");
			rbSessoes.setBounds(175, 86, 85, 23);
			getContentPane().add(rbSessoes);
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void setListener(ActionListener ac, FocusListener fl, MouseListener ml) {
        tfCod.addFocusListener(fl);
        tfCH.addFocusListener(fl);
        btNovo.addActionListener(ac);
        btExcluir.addActionListener(ac);
        btListar.addActionListener(ac);	
        rbHoras.addActionListener(ac);	
        rbSessoes.addActionListener(ac);
	}

	@Override
	public void limpar(){
		tfCod.setText("");
		tfNome.setText("");
		tfCH.setText("");
		rbHoras.setSelected(true);
		rbSessoes.setSelected(false);
		btExcluir.setEnabled(false);
	}
	
	@Override
	public void setDados(Object obj){
		Atividade atv = (Atividade) obj;
		tfCod.setText(atv.getCodigo());
		tfNome.setText(atv.getNome());
		tfCH.setText(String.valueOf(atv.getCarga()));
		if(atv.getTpCarga() == 0){
			rbHoras.setSelected(true);
			rbSessoes.setSelected(false);
		}else{
			rbHoras.setSelected(false);
			rbSessoes.setSelected(true);
		}
	}

	@Override
	public Atividade getDados(){
		Atividade atv = new Atividade();
		atv.setCodigo(tfCod.getText());
		atv.setNome(tfNome.getText());
		atv.setCarga(Integer.valueOf(tfCH.getText().trim()));
		if(rbHoras.isSelected())
			atv.setTpCarga(0);
		else
			atv.setTpCarga(1);
		atv.setUsuario(Usuario.getUsuario(GlobalDeclarations.Is_Usuario));
		atv.setDtOpe(new Date(System.currentTimeMillis()));
		return atv;
	}

	@Override
	public JButton getBtNovo() {
		return btNovo;
	}

	@Override
	public JButton getBtExcluir() {
		return btExcluir;
	}

	@Override
	public JButton getBtListar() {
		return btListar;
	}

}
